﻿using AJWPFAdmin.Modules.Main.Views;
using AJWPFAdmin.Core.Components.AJTable.ViewModels;
using AJWPFAdmin.Core.Utils;
using AJWPFAdmin.Services;
using AJWPFAdmin.Services.EF;
using Masuit.Tools.Models;
using MaterialDesignExtensions.Controls;
using MaterialDesignThemes.Wpf;
using Microsoft.EntityFrameworkCore;
using Prism.Commands;
using Prism.Events;
using Prism.Mvvm;
using Prism.Regions;
using Prism.Services.Dialogs;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using static AJWPFAdmin.Core.ExceptionTool;
using AJWPFAdmin.Core.Validation;
using Masuit.Tools;
using AJWPFAdmin.Core.Enums;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using AJWPFAdmin.Core;
using Masuit.Tools.Systems;
using Newtonsoft.Json;
using AJWPFAdmin.Core.Components.AJFilePicker.ViewModels;
using AJWPFAdmin.Core.ExtensionMethods;
using Prism.Ioc;
using LiveChartsCore;
using LiveChartsCore.SkiaSharpView;
using Masuit.Tools.DateTimeExt;
using LiveChartsCore.SkiaSharpView.WPF;
using System.Windows;
using LiveChartsCore.VisualElements;
using LiveChartsCore.SkiaSharpView.VisualElements;
using LiveChartsCore.SkiaSharpView.Painting;
using SkiaSharp;
using System.Windows.Media;
using AJWPFAdmin.Core.Mvvm;
using AJWPFAdmin.Core.Models.EF.Tables;
using AJWPFAdmin.Services.Models;

namespace AJWPFAdmin.Modules.Main.ViewModels
{
    public class ShippingRecordDetailViewModel : ViewModelBase, IDialogAware
    {

        private ShippingRecord _data;
        public ShippingRecord Data
        {
            get { return _data; }
            set { SetProperty(ref _data, value); }
        }

        private DbService db;
        private IRegionManager _regionMgr;
        private AJGlobalExceptionResolvedEvent _globalExceptionEvt;

        public ShippingRecordDetailViewModel(DbService dbIns,
            IEventAggregator eventAggregator)
        {
            db = dbIns;
        }


        public string Title => string.Empty;

        public event Action<IDialogResult> RequestClose;

        public bool CanCloseDialog()
        {
            return true;
        }

        public void OnDialogClosed()
        {

        }

        public void OnDialogOpened(IDialogParameters parameters)
        {
            parameters.TryGetValue<ShippingRecord>("data", out var data);

            // 复制一份数据
            var copy = CommonUtil.TryGetJSONObject<ShippingRecord>(CommonUtil.AJSerializeObject(data));

            // 重新动态处理文件路径,因为已经改成只存文件名了 --阿吉 2025年1月10日11点28分
            var _carIdentificationConfig = CarIdentificationConfigExt.GetCarIdentificationConfig(db);

            var namePlate = copy.GetFilesOfPropertyName(nameof(Car.Nameplate));
            copy.Nameplate = CommonUtil.AJSerializeObject(namePlate.Select(p => _carIdentificationConfig.CompleteFileUrl(copy.CarNo, p)));

            var engineNameplate = copy.GetFilesOfPropertyName(nameof(Car.EngineNameplate));
            copy.EngineNameplate = CommonUtil.AJSerializeObject(engineNameplate.Select(p => _carIdentificationConfig.CompleteFileUrl(copy.CarNo, p)));

            var machineEnvProtectionLabel = copy.GetFilesOfPropertyName(nameof(Car.MachineEnvProtectionLabel));
            copy.MachineEnvProtectionLabel = CommonUtil.AJSerializeObject(machineEnvProtectionLabel.Select(p => _carIdentificationConfig.CompleteFileUrl(copy.CarNo, p)));

            var emissionStandardImages = copy.GetFilesOfPropertyName(nameof(Car.EmissionStandardImages));
            copy.EmissionStandardImages = CommonUtil.AJSerializeObject(emissionStandardImages.Select(p => _carIdentificationConfig.CompleteFileUrl(copy.CarNo, p)));

            var vehicleLicense = copy.GetFilesOfPropertyName(nameof(Car.VehicleLicense));
            copy.VehicleLicense = CommonUtil.AJSerializeObject(vehicleLicense.Select(p => _carIdentificationConfig.CompleteFileUrl(copy.CarNo, p)));

            var attachments = copy.GetFilesOfPropertyName(nameof(Car.Attachments));
            copy.Attachments = CommonUtil.AJSerializeObject(attachments.Select(p => _carIdentificationConfig.CompleteFileUrl(copy.CarNo, p)));

            var carImgs = copy.GetFilesOfPropertyName(nameof(Car.CarImage));
            copy.CarImage = CommonUtil.AJSerializeObject(carImgs.Select(p => _carIdentificationConfig.CompleteFileUrl(copy.CarNo, p)));

            var inCaptureFile = copy.GetFilesOfPropertyName(nameof(ShippingRecord.InCaptureFile));
            copy.InCaptureFile = CommonUtil.AJSerializeObject(inCaptureFile.Select(p => _carIdentificationConfig.CompleteFileUrl(copy.CarNo, p)));

            var outCaptureFile = copy.GetFilesOfPropertyName(nameof(ShippingRecord.OutCaptureFile));
            copy.OutCaptureFile = CommonUtil.AJSerializeObject(outCaptureFile.Select(p => _carIdentificationConfig.CompleteFileUrl(copy.CarNo, p)));

            Data = copy;


        }

    }


}

